function PriorityQueue() {
  // 创建一个内部类
  function QueueElement(element, priority) {
    this.element = element
    this.priority = priority
  }
  this.items = []

  // 实现插入方法
  PriorityQueue.prototype.enqueue = function (element, priority) {
    // 创建QueueElement对象
    var queueElement = new QueueElement(element, priority)
    // 判断队列是否为零
    if (this.items.length == 0) {
      this.items.push(queueElement)
    } else {
      var added = false
      for (var i = 0; i < this.items.length; i++) {
        if (queueElement.priority < this.items[i]) {
          this.items.splice(i, 0, queueElement)
          added = true
          break
        }
      }
      if (!added) {
        this.items.push(queueElement)
      }
    }
  }
  // *******************************************

  // 2、删除前端元素，shift()函数在前面删除并返回该元素
  QueueElement.prototype.dequeue = function () {
    return this.items.shift()
  }
  // 3、查看前端的元素
  QueueElement.prototype.front = function () {
    return this.items[0]
  }
  // 4、查看队列是否为空
  QueueElement.prototype.isEmpty = function () {
    return this.items.length === 0
  }
  // 5、查看队列中的元素个数
  QueueElement.prototype.size = function () {
    return this.items.length
  }
  // 6、toString方法
  QueueElement.prototype.toString = function () {
    var resultString = ''
    for (var i = 0; i < this.items.length; i++) {
      resultString += this.items[i] + ' '
    }
    return resultString
  }

  // **************************************************
}

var pq = new PriorityQueue()
